<?php

    /**
     * Class AjaxController
     */
    class AjaxController extends AppController {

        var $uses = array( "User", "Theme", "Task", "Stage" );

        public function beforeFilter() {
            parent::beforeFilter();
            $this->layout = 'ajax';
            $this->view   = 'index';
        }

        public function beforeRender() {
            die();
        }

        public function index() {
        }

        /**
         * @param $id
         * sterge cerinta cu id-ul primit ca parametru
         */
        public function stergere_cerinta( $id ) {
            $this->Requirement->delete( $id );
        }

        /**
         * @param $id
         * sterge task-ul cu id-ul primit ca parametru
         */
        public function stergere_task( $id ) {
            $this->Task->delete( $id );
        }

        /**
         * @param $id_task
         */
        private function send_email_notification( $id_task ) {
            $theme = $this->Task->find( 'first', array(
                "conditions" => array( "Task.id" => $id_task ),
                "recursive"  => 2
            ) );
            self::send_email_to_profesor( $theme['Theme']['id_user'] );
            self::send_email_to_student( $theme['Theme']['id_stud'] );
        }

        /**
         * @param     $id
         *
         * @param int $checked
         *
         * @return int : -1-> eroare, 0-> nemarcat, 1-> marcat de prof, 2-> marcat de amandoi, 3-> marcat de student
         * este apelata de ajax marcheaza ca rezolvata sau nu o anumita sarcina in cadrul unei etape a temei de catre student
         */
        public function check_task_student( $id, $checked = 1 ) {
            $this->Task->set( array( "id" => $id, "check_student" => $checked ) );
            if( $this->Task->save() ) {
                $task = $this->Task->find( 'first', array( "conditions" => array( "Task.id" => $id, ) ) );
                switch( $checked ) {
                    case 1:
                        if( $task['Task']['check_profesor'] == 1 ) {
                            //marcat de amandoi
                            echo 2;
                        }
                        else {
                            //marcat doar de student
                            echo 3;
                        }
                        break;
                    case 0:
                        if( $task['Task']['check_profesor'] == 1 ) {
                            //marcat doar de profesor
                            echo 1;
                        }
                        else {
                            //nemarcat
                            echo 0;
                        }
                        break;
                }
            }
            else {
                echo - 1;
            }
        }

        /**
         * @param null $id
         * @param int  $checked
         *  * este apelata de ajax marcheaza ca rezolvata sau nu o anumita sarcina in cadrul unei etape a temei de catre student
         */
        public function check_stage_profesor( $id = null, $checked = 1 ) {
            $this->Stage->set( "done", $checked );
            $this->Stage->set( "id", $id );
            if( $this->Stage->save() ) {
                echo $checked;
            }
            else {
                echo - 1;
            }
        }

        /**
         * @param null $id
         * @param int  $checked
         * este apelata de ajax marcheaza ca rezolvata sau nu o anumita sarcina i cadrul unei etape a temei
         */
        public function check_task_profesor( $id = null, $checked = 1 ) {
            $this->Task->set( "check_profesor", $checked );
            $this->Task->set( "id", $id );
            if( $this->Task->save() ) {
                $task = $this->Task->find( "first", array( "conditions" => array( "Task.id" => $id ) ) );
                switch( $checked ) {
                    case 1:
                        if( $task['Task']['check_student'] == 0 ) {
                            //marcat doar de profesor
                            echo 1;
                        }
                        else {
                            //marcat de amandoi
                            echo 2;
                        }
                        break;
                    case 0:
                        if( $task['Task']['check_student'] == 0 ) {
                            //nemarcat
                            echo 0;
                        }
                        else {
                            //marcat de student
                            echo 3;
                        }
                        break;
                }
            }
            else {
                echo - 1;
            }
        }


    }